/*============================================================================*/

* Do: Creation Outcome Variables 

/*============================================================================*/

* ==============================================================================
* SECTION: DIRECTORIES
* ==============================================================================



if 		"`c(username)'" == "INSERT YOUR USERNAME HERE" {
			global DIR "INSERT YOUR PATH HERE"					
		}


cd "$DIR"

global RAW		"$DIR"
global CLEAN	"$DIR"
global OUT 		"$DIR"


* ==============================================================================
* SECTION: GENERATE VARIABLES FOR ANALYSIS - HH DATA
* ==============================================================================

use "$CLEAN/master_admin_all.dta", clear

*------------------------ BASELINE VARIABLES FOR HHS BALANCE
* CREATE VARIABLES FOR VILLAGE BALANCE 
* Number of Households
rename nb_hh BLnbHH

* Distance to Nearest Bank
rename distance BLdistance

* Internet Coverage
gen BLinternet_coverage = (BLvill_c07 == 1)

* Gender (1 if female and 0 otherwise)
gen BLgender = (BLbrt_cov3 == 1) if BLbrt_cov3 != .

* Primary (1 if completed primary school and 0 otherwise)
gen BLprimary = (BLbrt_a06 == 2 | BLbrt_a06 == 72) if BLbrt_a06 != .

* High School (1 if completed high school and 0 otherwise)
gen BLhighschool = ((BLbrt_a06 >= 3 & BLbrt_a06 <= 6) | BLbrt_a06 == 73) if BLbrt_a06 != .

* Tertiary (1 if completed tertiary education and 0 otherwise) 
gen BLtertiary = ((BLbrt_a06 >= 12 & BLbrt_a06 <= 62) | BLbrt_a06 == 74) if BLbrt_a06 != .

rename BLbrt_business BLbusiness
rename BLbrt_agother BLagother

* Volunteer (1 if has been volunteer and 0 otherwise)
gen BLvolunteer = (BLbrt_j01 == 1) if BLbrt_j01 != .

* Financial Literacy (Percentage of correct answers)
gen correct_answer1 = (BLbrt_x01 == 48) 	if BLbrt_x01 != .
gen correct_answer2 = (BLbrt_x02 == 20000) 	if BLbrt_x02 != . 
gen correct_answer3 = (BLbrt_x03 == 4) 		if BLbrt_x03 != .
gen correct_answer4 = (BLbrt_x04 == 4) 		if BLbrt_x04 != .

egen BLcorrect_answers = rowtotal(correct_answer*) if correct_answer1 != . | correct_answer2 != . | correct_answer3 != . | correct_answer4 != .
	replace BLcorrect_answers = BLcorrect_answers/4
drop correct_answer*

* WEALTH
* Variables to construct wealth index (salary, coverage, education, smartphone and assets)
rename BLbrt_salary_current salary_current	
rename BLbrt_salary_past salary_past		
gen BLcoverageProblem	= (BLbrt_b10ax == 1) if BLbrt_b10ax != . & BLbrt_b10ax != 6
gen good_coverage		= (BLcoverageProblem == 0) if BLcoverageProblem != .
gen BLeduc = 1 if BLbrt_a06 == 1
	replace BLeduc = 2 if BLbrt_a06 == 72 | BLbrt_a06 == 2
	replace BLeduc = 3 if BLbrt_a06 == 73 | BLbrt_a06 == 4  | BLbrt_a06 == 3
	replace BLeduc = 4 if BLbrt_a06 == 74 | BLbrt_a06 == 5  | BLbrt_a06 == 6
	replace BLeduc = 5 if BLbrt_a06 == 60 | BLbrt_a06 == 61 | BLbrt_a06 == 62 | BLbrt_a06 == 12 | BLbrt_a06 == 14 | BLbrt_a06 == 15
	*2"elemn". 3"junior high", 4"senior high", 5"univ or adult edu"
gen BLSmartPhone = (BLbrt_b01x == 1 & (BLbrt_b06 == "B" | BLbrt_b06 == "AB")) if BLbrt_b06 != "" | BLbrt_b01x != .
	*A"Simple phone: can make calls but no internet", B"Smart phone: with access to internet"

*ASSETS INDEX
foreach var of varlist BLbrt_m01_a-BLbrt_m01_t {
	replace `var' = 0 if `var' == 3
}

pca BLbrt_m01_a-BLbrt_m01_t
predict assets_index


foreach y in salary_current salary_past BLSmartPhone BLeduc good_coverage assets_index {
    sum `y' 
    gen INDEX`y'=(`y'-`r(mean)')/`r(sd)'
}

* NUMBER NON MISSING VALUES
egen nmb = rownonmiss(salary_current salary_past BLSmartPhone BLeduc good_coverage assets_index)
egen BLwealth = rowtotal(INDEX*) if nmb > 0 
	replace BLwealth = BLwealth/nmb
	drop assets_index nmb INDEX* salary_current salary_past BLSmartPhone BLeduc good_coverage assets_index


* SCALE BETWEEN 0 AND 1
foreach var in BLwealth {
	sum `var'
	replace `var'=(`var'-r(min))/(r(max)-r(min))
}

* Mobile (1 if has mobile phone and 0 otherwise) 
gen BLmobile = (BLbrt_b01x == 1) if BLbrt_b01x != .        

* Computer (1 if has computer and 0 otherwise)
gen BLcomputer = (BLbrt_m01_l == 1) if BLbrt_m01_l != .

* Bank Transactions (1 if did transaction in the past 30 days and 0 otherwise) 
gen BLbankTrans = (BLbrt_f22_a <= 4 | BLbrt_f22_b <= 4 | BLbrt_f22_c <= 4 | BLbrt_f22_d <= 4 | BLbrt_f22_e <= 4 | BLbrt_f22_v <= 4) if BLbrt_f18 != . & BLbrt_f18 != 9

* Saving (1 if saves in the past year and 0 otherwise)
** Has a bank saving account
gen BLsavebank = (strpos(BLbrt_f17 , "A") > 0) if BLbrt_f17 != ""
	*A"Saving account", B"Checking account", C"House Mortgage"
	*D"Certificate of deposit", E"Letter of credit", F"Business loan"
	*G"Vehicle loan", H"Personal loan", I"Health/life insurance"
	*J"Micro credit", V"Other", W"None"

* Trusts State Bank (1 if trusts in state banks and 0 otherwise)
gen BLtrust_state_bank = BLbrt_e02 

* Trusts Non State Bank (1 if trusts in non state banks and 0 otherwise)
gen BLtrust_nonstate_bank = BLbrt_e03

* Knows Mobile Banking (1 if knows mobile banking and 0 otherwise)
gen BLknow_mobile_bk = (BLbrt_f01 == 1) if BLbrt_f01 != .

* Knows the agent (1 if knows the agent by name and 0 otherwise)
gen BLknows_agn = (BLbrt_c01 == 1) if BLbrt_c01 != .

* Closest the agent (1 if closest the agent (family or close friend) and 0 otherwise)
gen BLclose_agn=(strpos(BLbrt_c08 , "A") > 0 | strpos(BLbrt_c08 , "B") > 0) if BLknows_agn != .
	*A"He/she is a family member", B"He/she is a friend of mine", C"He/she is a friend of friends"
	*D"He/she is my employer", E"I am her/his employer", F"He/she is a client of mine"
	*G"I am his/her client", H"He/She is a neighbor", I"He/She is my landlord" 
	*J"I am his/her landlord", K"He/she goes to my same church/ mosque", L"He/she is my colleague"
	*V"Other"

* TARGETTING BY WEALTH
gen rich_hh = 0 if BLwealth != ., a(BLwealth) 
sum BLwealth, d
replace rich_hh = 1 if BLwealth > `r(p50)' & BLwealth != .

gen finlit = 0 if BLcorrect_answers != ., a(BLcorrect_answers) 
sum BLcorrect_answers, d
replace finlit = 1 if BLcorrect_answers >= `r(p50)' & BLcorrect_answers != .

*------------------------ LABEL FOR TABLES
label variable BLgender 				"Female"
label variable BLprimary 				"Highest Degree=Primary School"
label variable BLhighschool 			"Highest Degree=High School"
label variable BLtertiary 				"Highest Degree=Tertiary Education"
label variable BLbusiness 				"Main Occupation=Non Farm Business"
label variable BLagother 				"Main Occupation=Agriculture or Other"
label variable BLvolunteer 				"Volunteered in the Past Year"
label variable BLcorrect_answers 		"Financial Literacy (\% of Correct Answers)"
label variable BLwealth 				"Wealth Index"
label variable BLmobile 				"Has a Mobile Phone"
label variable BLcomputer 				"Has a Laptop"
label variable BLbankTrans 				"Made a Bank Transaction in the Last Month"
label variable BLsavebank 				"Has a Bank Saving Account"
label variable BLtrust_state_bank 		"Trust in State Banks (1 to 5)"
label variable BLtrust_nonstate_bank 	"Trust in Non State Banks (1 to 5)"
label variable BLknow_mobile_bk 		"Knows about Branchless Banking"
label variable BLknows_agn 				"Knows the Agent"
label variable BLclose_agn 				"Friend or Family of the Agent"

* ==============================================================================
* SECTION: TAKE-UP AND USAGE
* ==============================================================================

* Take-up (1 if takes-up bm products and 0 otherwise)
foreach i in 09 10{
	forvalues j = 1(1)8 {
		replace ELbrt_ar`i'_`j' = 0 if ELbrt_ar`i'_`j' == 3
	}
}

foreach i in 16 {
	foreach j in a b c d e f g h i j k v {
		replace ELbrt_ef`i'_`j' = 0 if ELbrt_ef`i'_`j' == 3
	}
}

foreach p in a b c d e f g h i j k v {
	replace ELbrt_ef19_`p'	= 0 if ELbrt_ef19_`p' == 3
}

egen EL_takeupBM_1 = rowtotal(ELbrt_ar09_* ELbrt_ar10_*) if ELbrt_ar00x != .
	replace EL_takeupBM_1 = 1 if EL_takeupBM_1 > 1 & EL_takeupBM_1 != .
egen EL_takeupBM_2 = rowtotal(ELbrt_ef16_a ELbrt_ef16_d ELbrt_ef19_a ELbrt_ef19_d) if ELbrt_ef01 != .
	replace EL_takeupBM_2 = 1 if EL_takeupBM_2 > 1 & EL_takeupBM_2 != .
gen EL_takeupBM = (EL_takeupBM_1 == 1 | EL_takeupBM_2 == 1) 
	replace EL_takeupBM = 1 if add_clients == 1
	replace EL_takeupBM = . if ELbrt_ar00x == . & ELbrt_ef01 == .
drop EL_takeupBM_*

*------------------------ 

**** NOTE: THE DATASET CONTAINING THE FOLLOWING VARIABLE IS NOT PUBLICLY AVAILABLE DUE TO CONFIDENCIALITY CONCERNS *****
**** WE HAVE COMMENTED OUT THIS SECTION TO ALLOW THE CODE TO RUN SMOOTHLY ***

/*
* Number of transaction according to the admin data
foreach var of varlist amount* ntrans* deposits* withdrawl* {
	replace `var' = 0 if `var' == . & add_clients == 0
}

gen ADntransBM = ntrans_ecash_Q5 + ntrans_simakmur_Q5 + ntrans_ecash_Q6 + ntrans_simakmur_Q6 + ntrans_ecash_Q7 + ntrans_simakmur_Q7 + ntrans_ecash_Q8 + ntrans_simakmur_Q8 if add_clients == 0
	** windsorize at 100 
	replace ADntransBM = ADntransBM
	replace ADntransBM = 100 if ADntransBM >= 100

* Amount IHS
gen amountBM = amount_ecash_Q5 + amount_ecash_Q6 + amount_ecash_Q7 + amount_ecash_Q8 + amount_simakmur_Q5 + amount_simakmur_Q6 + amount_simakmur_Q7 + amount_simakmur_Q8 if add_clients == 0
gen ADamount_ihs = ln(amountBM + ((amountBM^2 + 1)^0.5))

gen depositsBM = deposits_ecash_Q5 + deposits_ecash_Q6 + deposits_ecash_Q7 + deposits_ecash_Q8 + deposits_simakmur_Q5 + deposits_simakmur_Q6 + deposits_simakmur_Q7 + deposits_simakmur_Q8 if add_clients == 0
gen ADdeposits_ihs = ln(depositsBM + ((depositsBM^2 + 1)^0.5))

* Balance IHS 
replace balance_ecash_Q8 = 0 if balance_ecash_Q8 < 0
replace balance_simakmur_Q8 = 0 if balance_simakmur_Q8 < 0
egen balance_admin = rowtotal(balance_ecash_Q8 balance_simakmur_Q8) if add_clients == 0
	replace balance_admin = 0 if balance_admin < 0
gen ADbalance_ihs=ln(balance_admin + ((balance_admin^2 + 1)^0.5)) if add_clients == 0
*/

* SAVING IHS 
* Branch
gen saving_amount_branch = (ELbrt_g03aa)
	replace saving_amount_branch = 0 if saving_amount_branch == .
	replace saving_amount_branch = . if ELbrt_g01 == . | (strpos(ELbrt_g03ax, "A") > 0 & saving_amount_branch == 0) | strpos(ELbrt_g03ax, "X") > 0 | ELbrt_g03aa == 999999999997 | ELbrt_g03aa == 999999999998 | ELbrt_g03aa == 999999999999
	*A"Formal bank account", B"Electronic savings account", C"Hiding place at home"
	*D"With friends or family", E"Cooperative", F"Informal saving network"
	*G"Baitul Maal Wat Tamwil (BMT)", H"Rotating saving/credit associatons (arisan)", V"Other"
	*X"Refuse to answer"
* Ecash
gen saving_amount_ecash = (ELbrt_g03aj)
	replace saving_amount_ecash = 0 if saving_amount_ecash == .
	replace saving_amount_ecash = . if ELbrt_g01 == . | (strpos(ELbrt_g03ax, "J") > 0 & saving_amount_ecash == 0) | strpos(ELbrt_g03ax, "X") > 0  | ELbrt_g03aj == 999999999997 | ELbrt_g03aj == 999999999998 | ELbrt_g03aj == 999999999999
	*A"Formal bank account", B"Electronic savings account", C"Hiding place at home"
	*D"With friends or family", E"Cooperative", F"Informal saving network"
	*G"Baitul Maal Wat Tamwil (BMT)", H"Rotating saving/credit associatons (arisan)", V"Other"
	*X"Refuse to answer"
	
* Simakmur
gen saving_amount_simakmur = (ELbrt_g03ai)
	replace saving_amount_simakmur = 0 if saving_amount_simakmur == .
	replace saving_amount_simakmur = . if ELbrt_g01 == . | (strpos(ELbrt_g03ax, "I") > 0 & saving_amount_simakmur == 0) | strpos(ELbrt_g03ax, "X") > 0 | ELbrt_g03ai == 999999999997 | ELbrt_g03ai == 999999999998 | ELbrt_g03ai == 999999999999
	*A"Formal bank account", B"Electronic savings account", C"Hiding place at home"
	*D"With friends or family", E"Cooperative", F"Informal saving network"
	*G"Baitul Maal Wat Tamwil (BMT)", H"Rotating saving/credit associatons (arisan)", V"Other"
	*X"Refuse to answer"
	
egen saving_amount_BM = rowtotal(saving_amount_ecash saving_amount_simakmur)
	replace saving_amount_BM = . if saving_amount_ecash == . | saving_amount_simakmur == .
gen EL_saving_ihs = ln(saving_amount_BM + ((saving_amount_BM^2 + 1)^0.5))
gen EL_saving_log = ln(1+saving_amount_BM)

* Take-up ecash (1 if takes up ecash and 0 otherwise)
egen EL_ecash_1 = rowtotal(ELbrt_ar09_*) 
gen EL_ecash = (ELbrt_ef16_a == 1 | ELbrt_ef19_a == 1 | EL_ecash_1 > 0)
	replace EL_ecash = . if ELbrt_ar00x == . & ELbrt_ef01 == . // did not respond
	drop EL_ecash_1

* Take-up simakmur (1 if takes up simakmur and 0 otherwise)
egen EL_simakmur_1 = rowtotal(ELbrt_ar10_*)
gen EL_simakmur = (ELbrt_ef16_d == 1 | ELbrt_ef19_d == 1 | EL_simakmur_1 > 0)
	replace EL_simakmur = . if ELbrt_ar00x == . & ELbrt_ef01 == . // did not respond
	drop EL_simakmur_1

**** NOTE: THE DATASET CONTAINING THE FOLLOWING VARIABLE IS NOT PUBLICLY AVAILABLE DUE TO CONFIDENCIALITY CONCERNS *****
**** WE HAVE COMMENTED OUT THIS SECTION TO ALLOW THE CODE TO RUN SMOOTHLY ***

/*

* Transactions (Total Amount, IHS) Ecash
gen amountBM_ecash = amount_ecash_Q5 + amount_ecash_Q6 + amount_ecash_Q7 + amount_ecash_Q8 if add_clients == 0
gen ADamount_ihs_ecash = ln(amountBM_ecash + ((amountBM_ecash^2 + 1)^0.5))

* Transactions (Total Amount, IHS) Simakmur
gen amountBM_simakmur = amount_simakmur_Q5 + amount_simakmur_Q6 + amount_simakmur_Q7 + amount_simakmur_Q8 if add_clients == 0
gen ADamount_ihs_simakmur = ln(amountBM_simakmur + ((amountBM_simakmur^2 + 1)^0.5))

* Balance IHS Ecash
egen balance_admin_ecash = rowtotal(balance_ecash_Q8) if add_clients == 0
	replace balance_admin_ecash = 0 if balance_admin_ecash < 0
gen ADbalance_ihs_ecash = ln(balance_admin_ecash + ((balance_admin_ecash^2 + 1)^0.5)) if add_clients == 0

* Balance IHS Simakmur
egen balance_admin_simakmur = rowtotal(balance_simakmur_Q8) if add_clients == 0
	replace balance_admin_simakmur = 0 if balance_admin_simakmur < 0
gen ADbalance_ihs_simakmur = ln(balance_admin_simakmur + ((balance_admin_simakmur^2 + 1)^0.5)) if add_clients == 0

*/

*------------------------ LABEL FOR TABLES
label variable EL_takeupBM 				"Take-up"
*label variable ADntransBM 				"Transactions (Number)"
*label variable ADamount_ihs 			"Transactions Amount (IHS)"
*label variable ADdeposits_ihs 			"Deposit Amount (IHS)"
*label variable ADbalance_ihs 			"Balance (IHS)"
label variable EL_saving_ihs 			"Saving (IHS)"
label variable EL_ecash 				"Take-up"
label variable EL_simakmur 				"Take-up"
*label variable ADamount_ihs_ecash 		"Transactions Amount (IHS)"
*label variable ADbalance_ihs_ecash 		"Balance (IHS)"
*label variable ADamount_ihs_simakmur 	"Transactions Amount (IHS)"
*label variable ADbalance_ihs_simakmur 	"Balance (IHS)"

* ==============================================================================
* SECTION: AGENT EFFORT AND SALES STRATEGY 
* ==============================================================================
  
* Agent Effort (How many times Agent talk with respondent?)
gen EL_effort_agent_talk = ELbrt_ef07
	replace EL_effort_agent_talk = . if EL_effort_agent_talk >= 98

* Heard product from Agent
gen EL_heard_agn = (ELbrt_ef02_a == 1 | ELbrt_ef02_d == 1) if ELbrt_f02_a != .

* PCA Index Agent Effort
pca EL_effort_agent_talk EL_heard_agn
predict EL_pcaEffort

* Agent offers the product to the respondent
gen EL_offer_prod = (ELbrt_ef08 == 1 | ELbrt_ef12 == 1) if ELbrt_ef07 != .

* Agent try to convice to the respondent
gen EL_agn_convince = (ELbrt_ef11 <= 2 | ELbrt_ef15 <= 2) if ELbrt_ef07 != .

* PCA Index Agent Sales Strategy
pca EL_offer_prod  EL_agn_convince
predict EL_pcaStyle

** Scale between 0 and 1
foreach var in EL_pcaEffort EL_pcaStyle {
	sum `var'
	replace `var'=(`var'-r(min))/(r(max)-r(min))
}

*------------------------ LABEL FOR TABLES
label variable EL_effort_agent_talk 	"Number of Times Products are Advertised"
label variable EL_heard_agn 			"Learned about Products from Agent"
label variable EL_pcaEffort 			"Agent Effort (PC)"
label variable EL_offer_prod			"Products Offered by Agent"
label variable EL_agn_convince 			"Agent Pro-Actively Promoted Products"
label variable EL_pcaStyle 				"Agent Sales Strategy (PC)"

* ==============================================================================
* SECTION: CLIENT PERCEPTIONS 
* ==============================================================================

*------------------------ PERCEIVED AGENT EARNINGS
* Perceived Agent Earnings (1 if it is perceived as high or fair and 0 otherwise)
gen EL_agn_earning = 1 if ELbrt_ef35 == 1 | ELbrt_ef35 == 2
replace EL_agn_earning = 0 if ELbrt_ef35 == 3

*------------------------ PRODUCT
* Reliable Product (1 if product is reliable and 0 otherwise)
gen EL_reliable_product_ecash		= (ELbrt_et04 > 8) if ELbrt_et04 != .
gen EL_reliable_product_simakmur	= (ELbrt_et03 > 8) if ELbrt_et03 != .
gen EL_reliable_product   			= min(EL_reliable_product_ecash,EL_reliable_product_simakmur) if ELbrt_et04 != .

* Fees are reasonables (1-5)
gen EL_fees_BM = ELbrt_ef53 
replace EL_fees_BM = 6 - ELbrt_ef53
replace EL_fees_BM = . if ELbrt_ef53 == 98 | ELbrt_ef53 == 99

* Money is Safe (1-5)
replace ELbrt_ef52 = . if ELbrt_ef52 >= 98
gen EL_money_safe = 6 - ELbrt_ef52

* Safe Product (1 if product is safe and 0 otherwise)
gen EL_safe_product_ecash  		= (ELbrt_et02 > 8) if ELbrt_et02 != .
gen EL_safe_product_simakmur  	= (ELbrt_et01 > 8) if ELbrt_et01 != .
gen EL_safe_product  			= min(EL_safe_product_ecash, EL_safe_product_simakmur) if ELbrt_et02 != .

*------------------------ CONSTRUCT INDEX USING VARIABLES WITH NON MISSING VALUES, SO WE DON'T LOSE TOO MANY OBSERVATIONS	  
* nmb == 4
local var1 EL_reliable_product EL_fees_BM  EL_money_safe EL_safe_product
* nmb == 3
local var2 EL_reliable_product  EL_money_safe EL_safe_product 
local var3 EL_reliable_product  EL_fees_BM EL_safe_product 
* nmb == 2 
local var4 EL_reliable_product  EL_safe_product
local var5 EL_fees_BM  EL_money_safe

forv i=1/5 {
	pca `var`i''
	predict EL_pcaProduct_`i'
}

gen EL_pcaProduct = EL_pcaProduct_1
forv i = 2/5 {
	replace EL_pcaProduct = EL_pcaProduct_`i' if EL_pcaProduct == .
}
drop EL_pcaProduct_*

foreach var in ELbrt_ef38 ELbrt_ef39 ELbrt_ef40 ELbrt_ef41 ELbrt_ef42 ELbrt_ef43 {
	g know_`var' = 1 if `var' >= 0
			replace know_`var' = 0 if `var'_x == 3 
}
egen know_any = rmax(know_*)

* ==============================================================================
* SECTION: BANK
* ==============================================================================

* Confidence in Bank Mandiri (1-5)
gen EL_confidence_BM = ELbrt_t09

* Trusts in Banks of his village (1-5)
gen EL_trust_banks_village = ELbrt_ee01
replace EL_trust_banks_village = . if EL_trust_banks_village == 98

* Trusts in state banks (1-5)
gen EL_confidence_state_banks = ELbrt_e02 

*------------------------ CONSTRUCT INDEX USING VARIABLES WITH NON MISSING VALUES, SO WE DON'T LOSE TOO MANY OBSERVATIONS 	  
* nmb == 4
local var1 EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks 
* nmb == 3
local var2 EL_confidence_BM  EL_confidence_state_banks

forv i = 1/2 {
	pca `var`i''
	predict EL_pcaBank_`i'
}

gen EL_pcaBank = EL_pcaBank_1
	replace EL_pcaBank = EL_pcaBank_2 if EL_pcaBank == .
	drop EL_pcaBank_*

* ==============================================================================
* SECTION: AGENT
* ==============================================================================

* Trusts in agent (1-5)
gen EL_trust_agent = 6 - ELbrt_u03

* Agent is competent (1-10)
gen EL_competent_agent = ELbrt_u11 if ELbrt_u11 != 99

* Agent is Altruistic (1-10)
gen EL_altruism_agent = ELbrt_u07 

* Agent would not steal of respondent's wallet
gen EL_wallet_agent = (ELbrt_u08 == 3) if ELbrt_u08 != .

*------------------------ CONSTRUCT INDEX USING VARIABLES WITH NON MISSING VALUES, SO WE DON'T LOSE TOO MANY OBSERVATIONS
* nmb=4
local var1 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent
* nmb=3
local var2 EL_trust_agent EL_altruism_agent EL_wallet_agent

forv i = 1/2 {
	pca `var`i''
	predict EL_pcaAgent_`i'
}

gen EL_pcaAgent = EL_pcaAgent_1
replace EL_pcaAgent = EL_pcaAgent_2 if EL_pcaAgent == .

drop EL_pcaAgent_*

* ==============================================================================
* SECTION: ALL
* ==============================================================================

*------------------------ CONSTRUCT INDEX USING VARIABLES WITH NON MISSING VALUES, SO WE DON'T LOSE TOO MANY OBSERVATIONS
* nmb==11
local var1 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks EL_reliable_product EL_fees_BM  EL_money_safe EL_safe_product
* nmb==10
local var2 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks EL_reliable_product   EL_money_safe EL_safe_product
local var3 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks EL_reliable_product EL_fees_BM   EL_safe_product
local var4 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM   EL_confidence_state_banks EL_reliable_product EL_fees_BM  EL_money_safe EL_safe_product
* nmb==9
local var5 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks EL_reliable_product  EL_safe_product
local var6 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_trust_banks_village EL_confidence_state_banks  EL_fees_BM  EL_money_safe
local var7 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM   EL_confidence_state_banks EL_reliable_product   EL_money_safe EL_safe_product
local var8 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM   EL_confidence_state_banks EL_reliable_product EL_fees_BM  EL_safe_product
* nmb==8
local var9 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_confidence_state_banks EL_reliable_product  EL_safe_product
* nmb==7
local var10 EL_trust_agent  EL_altruism_agent EL_wallet_agent EL_confidence_BM  EL_confidence_state_banks EL_reliable_product  EL_safe_product
* nmb==4
local var11 EL_trust_agent EL_competent_agent EL_altruism_agent EL_wallet_agent

forv i = 1/11 {
	pca `var`i''
	predict EL_pcaAll_`i'
}

gen EL_pcaAll = EL_pcaAll_1

forval i = 2/11 {
	replace EL_pcaAll = EL_pcaAll_`i' if EL_pcaAll == .
}
drop EL_pcaAll_*

* Scale between 0 and 1
foreach var in EL_pcaProduct EL_pcaBank EL_pcaAgent EL_pcaAll {
	sum `var'
	replace `var'=(`var'-r(min))/(r(max)-r(min))
}

*------------------------ LABEL FOR TABLES
label variable EL_agn_earning 				"Perceived Agent Earnings"
label variable EL_reliable_product 			"Products are Reliable"
label variable EL_fees_BM 					"Fees are Reasonable"
label variable EL_money_safe 				"Money is Safe"
label variable EL_safe_product 				"Product is Safe"
label variable EL_pcaProduct 				"Trust in Product (PC)"
label variable EL_confidence_BM 			"Confidence in The Bank"
label variable EL_trust_banks_village 		"Trust Banks in the Village"
label variable EL_confidence_state_banks 	"Contracts with Bank are Enforced"
label variable EL_pcaBank 					"Trust in Bank (PC)"
label variable EL_trust_agent 				"Trusts Agent"
label variable EL_competent_agent 			"Agent is Competent"
label variable EL_altruism_agent 			"Agent is Altruistic"
label variable EL_wallet_agent 				"Agent Would not Steal Wallet"
label variable EL_pcaAgent 					"Trust in Agent (PC)"
label variable EL_pcaAll 					"Trust All (PC)"
label variable know_any 					"Knows Commission earned by Agent"

* ==============================================================================
* SECTION: HETEROGENEUS EFFECT
* ==============================================================================

*------------------------ AGENT CHARACTERISTICS
* Agent is rich
foreach var of varlist BLagn_s01_a-BLagn_s01_t {
	replace `var' = 0 if `var' == 3
}

pca BLagn_s01_a-BLagn_s01_t
predict agn_assets_index

sum agn_assets_index, detail

* ==============================================================================
* SECTION: RESPONDENT TRUST AND KNOWLEDGE
* ==============================================================================

* Respondent Knows and close the Agent
tab BLclose_agn // already generated in Section 1

* Respondent Trusts the Agent Financial Advice a lot
replace BLbrt_c15 = . if BLbrt_c15 == 8
gen BLtrust_AGNadvice=(BLbrt_c15 >= 4 & BLbrt_c15 != .) if BLbrt_c01 != . 
 
*------------------------ LABEL FOR TABLES
label variable BLtrust_AGNadvice 	"Respondent Trusts the Agent"

* ==============================================================================
* SECTION: SPILLOVERS 
* ==============================================================================

* Take-up Other Branchless Banking Products
egen EL_takeupnoBM_mb = rowtotal(ELbrt_ef16_b ELbrt_ef16_c  ELbrt_ef16_e ELbrt_ef16_f ELbrt_ef16_g ELbrt_ef16_h ///
ELbrt_ef16_i ELbrt_ef16_j ELbrt_ef16_k ELbrt_ef16_v ELbrt_ef19_b ELbrt_ef19_c  ELbrt_ef19_e ELbrt_ef19_f ///
ELbrt_ef19_g ELbrt_ef19_h ELbrt_ef19_i ELbrt_ef19_j ELbrt_ef19_k ELbrt_ef19_v)

	replace EL_takeupnoBM_mb = 1 if EL_takeupnoBM_mb > 1
	replace EL_takeupnoBM_mb = .  if ELbrt_ef01 == . // did not respond

							
* Take-up Other Financial Products
gen EL_other_fin_prods = 0 if ELbrt_f17 == "W"
	replace EL_other_fin_prods = 1 if ELbrt_f17 != "" & ELbrt_f17 != "W"
	*A"Saving account", B"Checking account", C"House Mortgage"
	*D"Certificate of deposit", E"Letter of credit", F"Business loan"
	*G"Business loan", H"Personal loan", I"Health/life insurance"
	*J"Micro credit", V"Other", W"None"

* Has a bank account
foreach var of varlist ELbrt_f18_* {
	replace `var' = 0 if `var' == 3 | `var' == 9
}

egen EL_bank = rowtotal(ELbrt_f18_*) if  ELbrt_f18_a != .
replace EL_bank = (EL_bank>0) 
replace EL_bank = . if ELbrt_f18_a == .

* Saving in any bank
gen EL_saving_branch_ihs = ln(saving_amount_branch + ((saving_amount_branch^2 + 1)^0.5))

*------------------------ LABEL FOR TABLES
label variable EL_takeupnoBM_mb 		"Take-up of Branchless Banking Products from Other Banks"
label variable EL_other_fin_prods 		"Take-up of Other Financial Products"
label variable EL_bank 					"Has a Bank Account in Any Bank"
label variable EL_saving_branch_ihs 	"Total Saving in Any Bank (IHS)"

* ==============================================================================
* SECTION: TRUST VARIABLES BASELINE
* ==============================================================================

** Agent
gen BLcomputer_agn = (BLagn_b14 == 1) if BLagn_b14 != .

* ==============================================================================
* SECTION: TIME AND USE SPECIFIC INVESTMENTS
* ==============================================================================

* Correction
bysort agn_cl: egen aux = max(ELagn_ed15d) 
drop aux

* Time use: E15 How many hours per week do you spend in [_…]_?
gen 	ELagn_hrs_BMjob=ELagn_ed15d

* Time us: specific tasks in the BM job
gen	ELagn_hrs_promotingshop=ELagn_ed15d*ELagn_ed16a/100
gen	ELagn_hrs_promotingoutshop=ELagn_ed15d*ELagn_ed16b/100
gen	ELagn_hrs_transact=ELagn_ed15d*ELagn_ed16c/100
gen	ELagn_hrs_internet=ELagn_ed15d*ELagn_ed16d/100
gen	ELagn_hrs_cash=ELagn_ed15d*ELagn_ed16e/100
gen	ELagn_hrs_educ=ELagn_ed15d*ELagn_ed16f/100

foreach y of varlist  ELagn_hrs_BMjob ELagn_hrs_promotingshop	ELagn_hrs_promotingoutshop	ELagn_hrs_transact 	ELagn_hrs_internet	ELagn_hrs_cash	ELagn_hrs_educ {
		gen	`y'_any=(`y'>0)
			replace `y'_any=. if `y'==.
}

* Have you done any specific investments to improve your performance as a BM agent? 
gen	ELagn_invest_computer=(ELagn_ed18=="A" | ELagn_ed18=="AB" | ELagn_ed18=="ABDE" | ELagn_ed18=="ABE" | ELagn_ed18=="AD" | ELagn_ed18=="AE")
	replace ELagn_invest_computer=. if ELagn_ed18==""
gen	ELagn_invest_banner=(ELagn_ed18=="B" | ELagn_ed18=="AB" | ELagn_ed18=="ABDE" | ELagn_ed18=="ABE" | ELagn_ed18=="BC" | ELagn_ed18=="BD" | ELagn_ed18=="BDE" | ELagn_ed18=="BE" | ELagn_ed18=="BEV" | ELagn_ed18=="BV")
	replace ELagn_invest_banner=. if ELagn_ed18==""
gen	ELagn_invest_hired=(ELagn_ed18=="C" | ELagn_ed18=="BC" | ELagn_ed18=="CD" | ELagn_ed18=="CE" | ELagn_ed18=="CV")
	replace ELagn_invest_hired=. if ELagn_ed18==""
gen	ELagn_invest_compinternet=(ELagn_ed18=="D" | ELagn_ed18=="AD" | ELagn_ed18=="BD" | ELagn_ed18=="BDE" | ELagn_ed18=="CD" | ELagn_ed18=="DE")
	replace ELagn_invest_compinternet=. if ELagn_ed18==""
gen	ELagn_invest_dataplan=(ELagn_ed18=="E" | ELagn_ed18=="ABDE" | ELagn_ed18=="ABE" | ELagn_ed18=="AE" | ELagn_ed18=="BDE" | ELagn_ed18=="BE" | ELagn_ed18=="BEV" | ELagn_ed18=="CE" | ELagn_ed18=="DE" | ELagn_ed18=="EV")
	replace ELagn_invest_dataplan=. if ELagn_ed18==""
gen	ELagn_invest_other=(ELagn_ed18=="V" | ELagn_ed18=="BEV" | ELagn_ed18=="BV" | ELagn_ed18=="CV" | ELagn_ed18=="EV")
	replace ELagn_invest_other=. if ELagn_ed18==""

gen	ELagn_invest_any=(ELagn_ed18!="W")
	replace ELagn_invest_any=. if ELagn_ed18==""
gen	ELagn_invest_any2=(ELagn_ed18!="W" & ELagn_invest_other!=1)
	replace ELagn_invest_any=. if ELagn_ed18==""
	
	*A"Bought / upgraded computers", B"Made banners/leaflets, etc. ", C"Hired someone else to help"
	*D"Bought a new computer/phone/tablet/ with better internet signa", E"Bought data plan", V"Other"


* Order
*order agn_cl dcode* svar* high_private high_public low_private low_private low_public high low private public BL* EL* AD*
order agn_cl dcode* svar* high_private high_public low_private low_private low_public high low private public BL* EL* 


save "$OUT/master_admin_final.dta", replace

* ==============================================================================
* SECTION: VILLAGE LEVEL: ALL CLIENTS ADMIN DATA
* ==============================================================================

use "$CLEAN/admin_data_vill_all.dta", clear 

*------------------------ BASELINE VARIABLES
* Number of Households
rename nb_hh BLnbHH

* Distance to Nearest Bank
rename distance BLdistance

* Internet Coverage
gen BLinternet_coverage = (BLvill_c07 == 1)

* Gender (1 if Female and 0 otherwise)
gen BLgender = (BLagn_a02a == 3) if BLagn_a02a != .

* Primary (1 if completed primary school and 0 otherwise)
gen BLprimary = (BLagn_a06 == 2 | BLagn_a06 == 72) if BLagn_a06 != .

* High School (1 if completed high school and 0 otherwise)
gen BLhighschool = ((BLagn_a06 >= 3 & BLagn_a06 <= 6) | BLagn_a06 == 73) if BLagn_a06 != .

* Tertiary (1 if completed tertiary education and 0 otherwise) 
gen BLtertiary = ((BLagn_a06 >= 12 & BLagn_a06 <= 62) | BLagn_a06 == 74) if BLagn_a06 != .

* Main Occupation
 
rename BLagn_business BLbusiness
rename BLagn_agother BLagother

* Volunteer (1 if has been volunteer and 0 otherwise) 
gen BLvolunteer = (BLagn_p01 == 1) if BLagn_p01 != .

* Financial Literacy (Percentage of correct answers)
gen correct_answer1 = (BLagn_t01a == 48) 	if BLagn_t01a !=.
gen correct_answer2 = (BLagn_t01  == 20000) if BLagn_t01  !=. 
gen correct_answer3 = (BLagn_t02  == 4) 	if BLagn_t02  !=.
gen correct_answer4 = (BLagn_t03  == 3) 	if BLagn_t03  !=.

egen BLcorrect_answers = rowtotal(correct_answer*) if correct_answer1 != . | correct_answer2 != . | correct_answer3 != . | correct_answer4 != .
	replace BLcorrect_answers = BLcorrect_answers/4

* WEALTH
* Variables to construct wealth index (salary, coverage, education, smartphone and assets)
rename BLagn_salary_current salary_current

gen agn_educ = 1 if BLagn_a06 == 1
	replace agn_educ = 2 if BLagn_a06 == 72 | BLagn_a06 == 2
	replace agn_educ = 3 if BLagn_a06 == 73 | BLagn_a06 == 4  |BLagn_a06 == 3
	replace agn_educ = 4 if BLagn_a06 == 74 | BLagn_a06 == 5  |BLagn_a06 == 6
	replace agn_educ = 5 if BLagn_a06 == 60 | BLagn_a06 == 61 |BLagn_a06 == 62 | BLagn_a06 == 12 | BLagn_a06 == 14 | BLagn_a06 == 15

* ASSET INDEX
foreach var of varlist BLagn_s01_a-BLagn_s01_t {
	replace `var' = 0 if `var' == 3
}

pca BLagn_s01_a-BLagn_s01_t
predict assets_index

foreach y in salary_current  agn_educ assets_index {
    su `y' 
    gen INDEX`y' = (`y'-`r(mean)')/`r(sd)'
}

* NUMBER NON MISSING VALUES
egen nmb = rownonmiss(salary_current  agn_educ assets_index)
egen BLwealth = rowtotal(INDEX*) if nmb>0 
replace BLwealth = BLwealth/nmb

* Scale between 0 and 1
foreach var in BLwealth {
	sum `var'
	replace `var'=(`var'-r(min))/(r(max)-r(min))
}

* Mobile (1 if has mobile phone and 0 otherwise) 
gen BLmobile = ((BLagn_b06 == "B" | BLagn_b06 == "AB" | BLagn_b06 == "A")) if BLagn_b06 != "" 
	*A"Regular mobile phone (for phone calls but do not have any internet access)", B"Smartphone (have internet access)"

* Computer (1 if has computer and 0 otherwise)
gen BLcomputer = (BLagn_b14 == 1) if BLagn_b14 != .

*------------------------ LABEL FOR TABLES
label variable BLnbHH 					"Number HHs"
label variable BLdistance 				"Distance to Nearest Bank Branch (in km)"
label variable BLinternet_coverage 		"Internet Coverage"
label variable BLgender 				"Female"
label variable BLprimary 				"Highest Degree=Primary School"
label variable BLhighschool 			"Highest Degree=High School"
label variable BLtertiary 				"Highest Degree=Tertiary Education"
label variable BLbusiness 				"Main Occupation=Non Farm Business"
label variable BLagother 				"Main Occupation=Agriculture or Other"
label variable BLvolunteer 				"Volunteered in the Past Year"
label variable BLcorrect_answers 		"Financial Literacy (\% of Correct Answers)"
label variable BLwealth 				"Wealth Index"
label variable BLmobile 				"Has a Mobile Phone"
label variable BLcomputer 				"Has a Laptop"

* Order
order agn_cl dcode* svar* high_private high_public low_private low_private low_public high low private public BL*

save "$OUT/admin_data_vill_final_all.dta", replace


* ==============================================================================
* SECTION: LONG TERM DATA
* ==============================================================================

**** NOTE: THIS DATASET IS NOT PUBLICLY AVAILABLE DUE TO CONFIDENCIALITY CONCERNS ****
**** WE COMMENT OUT THIS SECTION TO MAKE THE CODE RUN SMOOTHLY ****

/*
* MERGE TO FINAL DATASET
use "$RAW/longterm_agentlevel.dta", clear

cap gen low_private = 1 if (high_private & high_public & low_public) == 0
	replace low_private = 0 if (high_private | high_public | low_public) == 1
	replace agent = 0 if agent == .
	* Windsorize observations
	replace stillhas_bbagent = 0 if stillhas_bbagent == .
	replace currentbalance = 100 if  currentbalance > 100 & currentbalance != .
	replace averagebalance = 100 if averagebalance > 100  & averagebalance != .
	replace endingbalance = 1000 if endingbalance > 1000  & endingbalance != .
	replace salesvolume = 500 if salesvolume > 500 & salesvolume != .
	
gen med_freq_cl 		= med_freq/med_client
gen med_bal_cl 			= med_balance/med_client
gen asinh_med_bal_cl	= asinh(med_bal_cl)
gen med_sv_cl  			= med_sv/med_client
gen asinh_med_sv_cl 	= asinh(med_sv_cl)
gen avg_freq_cl 		= avg_freq/avg_client
gen avg_bal_cl  		= avg_balance/avg_client
gen asinh_avg_bal_cl	= asinh(avg_bal_cl)
gen avg_sv_cl  			= avg_sv/avg_client
gen asinh_avg_sv_cl 	= asinh(avg_sv_cl)
gen asinh_med_balance 	= asinh(med_balance)
gen asinh_med_sv 		= asinh(med_sv)

label var stillhas_bbagent 		"Still has an agent"
label var avg_balance 			"Average Balance"
label var med_balance 			"Median Balance"
label var avg_client 			"Average Num of Clients"
label var med_client 			"Median Num of Clients"
label var avg_freq 				"Average Frequency"
label var med_freq 				"Median Frequency"
label var avg_sv 				"Average Payments Volume"
label var med_sv 				"Median Payments Volume"
label var med_freq_cl 			"Transaction Frequency/Number of Clients"
label var asinh_med_bal_cl 		"Balance/Number of Clients (IHS)"
label var asinh_med_sv_cl 		"Payments/Number of Clients (IHS)"
label var avg_freq_cl 			"Transaction Frequency/Number of Clients"
label var asinh_avg_bal_cl 		"Balance/Number of Clients (IHS)"
label var asinh_avg_sv_cl 		"Payments/Number of Clients (IHS)"
label var asinh_med_balance 	"Balance (IHS)"
label var asinh_med_sv 			"Payments (IHS)"
label var med_freq 				"Transaction Frequency"
label var asinh_med_sv_cl 		"Payments/Number of Clients (IHS)"

*CREATE TAKE-UP RATE
merge 1:m agn_cl using "$OUT/master_admin_final.dta", nogen force
duplicates drop agn_cl, force

save "$OUT/longterm_agentlevel_final.dta", replace
*/
